//
//  TJStatusPicView.swift
//  WeiBo
//
//  Created by TanJian on 15/12/11.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit
import SnapKit


let TJStatusPicCellID = "TJStatusPicCellID"
let itemMargin: CGFloat = 5
let itemWH = (TJScreen.width - itemMargin * 2 - TJMargin * 2) / 3


class TJStatusPicView: UICollectionView {

    var sizeConstraint:Constraint?
    


    var pic_urls:[TJStatusPicInfo]? {
        didSet{

            
            let size = calcSize(pic_urls?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                self.sizeConstraint = make.size.equalTo(size).constraint
            }
            
            reloadData()
        }
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
//        setupUI()
        
        //设置collectionView
        //注册cell
        registerClass(TJStatusPicViewCell.self, forCellWithReuseIdentifier: TJStatusPicCellID)
        
        dataSource = self
        
        let flowout = self.collectionViewLayout as! UICollectionViewFlowLayout
        flowout.itemSize = CGSize(width: itemWH, height: itemWH)
        //水平间距
        flowout.minimumInteritemSpacing = itemMargin
        //垂直间距
        flowout.minimumLineSpacing = itemMargin
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    func calcSize(count:Int) -> CGSize {
        
        //行数和列数计算
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        let row = (count - 1) / 3 + 1
        //宽度
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: width, height: height)
        
    }
    
}

    
extension TJStatusPicView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(TJStatusPicCellID, forIndexPath: indexPath) as! TJStatusPicViewCell
        
        cell.picInfo = pic_urls![indexPath.item]
        
        return cell
    }
    
}

    
class TJStatusPicViewCell: UICollectionViewCell {
    
    //懒加载cell中控件
    private lazy var imgView: UIImageView = {
        
        let imageView = UIImageView()
        //图片显示模式为截取部分不缩放
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }()
    
    //git小图标
    private lazy var gifView : UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))

    var picInfo:TJStatusPicInfo? {
        didSet{
            if let urlStr = picInfo?.thumbnail_pic {
                gifView.hidden = !urlStr.hasSuffix(".gif")
                
                imgView.sd_setImageWithURL(NSURL(string: urlStr),placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        
        contentView.addSubview(imgView)
        contentView.addSubview(gifView)
        
        // 添加约束
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imgView)
            make.bottom.equalTo(imgView)
        }
    }
}
